import os
import pandas as pd


def initialize():  # 数据文件初始化
    flag = 0
    rootPath = os.getcwd()
    dic = []
    okList = []
    if os.path.isfile(rootPath + '/log'):
        with open('log', encoding='utf-8', mode='r') as f:
            f.readline()
            okList = eval(f.readline())
            f.readline()
            errorList = eval(f.readline())
        if (len(okList) == 0):
            flag = 1
            print('可能不存在完整的数据集！')
    else:
        flag = 1
    if (flag):
        makePath()
        # 数据初始化1
        print('读取数据文件：dataSet.csv')
        dataSet = pd.read_csv('dataSet.csv', encoding="gbk")  # 加载数据
        names = {
            '站点': 'zd', '年': 's', '月': 'y', '平均最低气温': 'dt',
            '平均最高气温': 'gt', '平均气温': 'mt', '降水量': 'js',
            '平均相对湿度': 'pxs', '平均风速': 'pfs', '日照时数': 'rs',
            '日照百分率': 'rb',
        }
        dataSet = dataSet.rename(columns=names)  # 重命名各列
        dataSet['s'] = dataSet['s']*100 + dataSet['y']  # 合并年月
        dataSet.pop('y')  # 删除'月'列
        print('分站点分割dataSet')
        dic = divFile(dataSet)
        print('剔除数据缺失的站点，存储csv文件')
        dic, okList = saveFile(dic)
    else:
        error = 1
        for i in okList:
            file = './dataSets/' + str(i) + '.csv'
            try:
                dic.append(pd.read_csv(file, encoding="utf-8"))
            except Exception:
                error = 0
                print(Exception)
        if error:
            print('dic读取成功')
    print('initialize返回dic, okList')
    return dic, okList
def makePath():
    path = os.getcwd() + '/dataSets'
    # 判断路径是否存在
    # 存在     True
    # 不存在   False
    flag = os.path.exists(path)
    if not flag:
        # 如果不存在则创建目录
        os.makedirs(path)  # 创建目录操作函数
        print(path + '创建成功')
def divFile(dataSet):  # 根据站点列分割dataSet文件
    length = len(dataSet['s'])  # 58963
    dic = {}
    x_1 = 0
    zd_1 = dataSet['zd'][0]
    for x_2 in range(length):
        zd_2 = dataSet['zd'][x_2]
        if zd_2 != zd_1:
            dic[zd_1] = dataSet[x_1:x_2]
            x_1 = x_2
            zd_1 = zd_2
    return dic
def saveFile(dic):
    okList = []
    errorList = []
    for i in dic:
        flag = 1
        dic[i] = dic[i].reset_index()  # 重新设置index
        dic[i].pop('index')  # 删除index
        dic[i].pop('zd')  # 删除'站点'列
        length = len(dic[i]['s'])
        if length != 72:  # 年份月份出错
            flag = 0
        for j in range(length):
            li = list(dic[i].loc[j])
            if 32766 in li:  # 剔除错误数据
                flag = 0
                break
        if (flag):
            okList.append(i)
            dic[i].to_csv('./dataSets/' + str(i) + '.csv', index=0)
        else:
            errorList.append(i)
    print('csv文件存储成功')
    if (len(errorList) > 0):
        print('存在缺失数据，删除中...')
        for i in errorList:
            del (dic[i])
    with open('log', encoding='utf-8', mode='w') as f:
        f.write('okList\n')
        f.write(str(okList)+'\n')
        f.write('errorList\n')
        f.write(str(errorList) + '\n')
    print('log文件存储成功')
    return dic, okList